package com.itheima.stock.handler;


import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.Exceptions;
import com.itheima.stock.exception.StockException;
import com.itheima.stock.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * GlobalExceptionHandler 全局异常处理类
 *
 * @author DanLi
 * @date 2025/7/13
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理自定义StockException异常
     *
     * @param e StockException
     * @return ResponseResult<?>
     */
    @ExceptionHandler(StockException.class)
    public ResponseResult<?> handleStockException(StockException e) {
        return ResponseResult.error(e.getCode());
    }

    /**
     * 处理未知Exceptions异常
     *
     * @param e Exceptions
     * @return ResponseResult<?>
     */
    @ExceptionHandler(value = Exceptions.class)
    public ResponseResult<?> handleExceptions(Exceptions e) {
        log.error("异常信息:{}", e.getMessage());
        //TODO
        return ResponseResult.error(CodeEnum.SYSTEM_ERROR);
    }
}
